#######################
function Get-ConnectionStringOracle
{
    param($ConnectionString, $dbmsName)

    #For Oracle no special handling just return the connection string
    New-Object PSObject -Property @{Name=$dbmsName;dbmsType='oracle';connectionString=$connectionString}

} #Get-ConnectionStringOracle

#######################
function Get-ConnectionStringSql
{
    param($ConnectionString, $dbmsName, $hasDatabases)

    #Create a connection string per online database excluding databases
    #listed in exclusion_lku
    if ($hasDatabases)
    {
        $exclude = @()
        $Script:Exclusion| where {$_.dbms_type_name -eq 'sqlserver' -and ($_.dbms_name -eq $dbmsName -or $_.dbms_name -eq 'ALL')} |
                                           foreach {$exclude +=,$_.database_name }
        Get-SqlDatabaseName $($ConnectionString -f 'master') | where {$exclude -notcontains $_.name} | 
        Select @{n='Name';e={"$dbmsName.{0}" -f $($_.name)}}, @{n='dbmsType';e={'sqlserver'}}, @{n='connectionString';e={"$ConnectionString" -f $($_.name)}}
    }
    else
    { New-Object PSObject -Property @{Name=$dbmsName;dbmsType='sqlserver';connectionString=$connectionString} }


} #Get-ConnectionStringSql

#######################
function Get-SqlDatabaseName
{
    param($ConnectionString)

    Get-OLEDBData -ConnectionString $ConnectionString -sql $Script:RSDDSql.sqlserver.GetDatabases

} #Get-SqlDatabaseName

#######################
function Get-ConnectionString
{
    param()

    $sql = "SELECT * FROM dbo.dbms_lku WHERE is_enabled = 1"

    try {
        foreach($dbms in Get-OLEDBData -ConnectionString $Script:OleDbConnString -sql $sql)
        {
            if ($dbms.is_encrypted)
            { $connString = Read-EncryptedString -InputString $($dbms.connection_string) -Password $Script:Password }
            else
            { $connString = $dbms.connection_string }
            
            switch ($dbms.dbms_type)
            {
                'sqlserver' { Get-ConnectionStringSql -ConnectionString $connString -dbmsName $dbms.dbms_name $dbms.has_databases}
                'oracle' { Get-ConnectionStringOracle -ConnectionString $connString -dbmsName $dbms.dbms_name}
            }
        }
    }
    catch {
      $ex = $_.Exception
      Write-LogMessage -Source 'Get-ConnectionString' -Message "$ex.Message"
      Write-Error "Get-ConnectionString $ex.Message"
      continue
    }

} #Get-ConnectionString
